﻿package com.tudou.home.model
{
    import com.tudou.cookie.*;
    import com.tudou.events.*;
    import com.tudou.home.commands.*;
    import com.tudou.home.track.*;
    import com.tudou.log.*;
    import com.tudou.model.*;
    import com.tudou.track.*;
    import com.tudou.v2.*;
    import flash.utils.*;
    import org.puremvc.as3.patterns.proxy.*;

    public class V2Proxy extends Proxy
    {
        private var playerEventTracker:PlayerEventTracker;
        private var v2Loader:V2Loader;
        public static const NAME:String = "V2Proxy";

        public function V2Proxy()
        {
            super(NAME);
            playerEventTracker = PlayerEventTracker.getInstance();
            v2Loader = new V2Loader();
            v2Loader.addEventListener(V2LoaderEvent.COMPLETE, v2LoadedHlr);
            v2Loader.addEventListener(V2LoaderEvent.IO_ERROR, v2IOErrHlr);
            v2Loader.addEventListener(V2LoaderEvent.SECURITY_ERROR, v2SecurityErrHlr);
            return;
        }// end function

        private function addNodeLog(param1:String, param2:Array = null) : void
        {
            AOPTracker.getInst().addNodeLog(param1, param2);
            return;
        }// end function

        public function load(param1:IBaseInfo, param2:String) : void
        {
            TudouLogger.debug(this, "Start Load V2:", getTimer());
            v2Loader.load(param1, param2);
            addNodeLog(NodeLog.V2_REQUEST_START);
            UserLog.getInst().addNode(NodeLog.V2_REQUEST_START, [encodeURIComponent(v2Loader.url)]);
            return;
        }// end function

        private function v2IOErrHlr(event:V2LoaderEvent) : void
        {
            addNodeLog(NodeLog.V2_REQUEST_ERROR);
            UserLog.getInst().addNode(NodeLog.V2_REQUEST_SECURITY_ERROR);
            return;
        }// end function

        private function v2SecurityErrHlr(event:V2LoaderEvent) : void
        {
            addNodeLog(NodeLog.V2_REQUEST_ERROR);
            UserLog.getInst().addNode(NodeLog.V2_REQUEST_ERROR);
            return;
        }// end function

        private function v2LoadedHlr(event:V2LoaderEvent) : void
        {
            TudouLogger.debug(this, "V2 Loaded:", getTimer());
            addNodeLog(NodeLog.V2_REQUEST_COMPLETE);
            UserLog.getInst().addNode(NodeLog.V2_REQUEST_COMPLETE, [encodeURIComponent(v2Loader.xml)]);
            var _loc_2:* = v2Loader.getVideoInfo();
            if (_loc_2.v2ErrorNo == 0)
            {
                sendNotification(V2CommandSet.INFO_LOADED, _loc_2);
            }
            else
            {
                addNodeLog(NodeLog.V2_XML_ERROR, [_loc_2.v2ErrorNo]);
                sendNotification(V2CommandSet.INFO_ERROR, _loc_2.v2ErrorNo);
            }
            return;
        }// end function

    }
}
